home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / BasicMenuUtilities.java < prev    next >
Text File  |  1998-06-30  |  8KB  |  254 lines

  1. /*
  2.  * @(#)BasicMenuUtilities.java    1.2 98/01/30
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20.  
  21. package com.sun.java.swing.plaf.basic;
  22. import com.sun.java.swing.*;
  23. import java.awt.*;
  24.  
  25.  
  26. /**
  27.  * A Windows L&F implementation of MenuUI.  This implementation 
  28.  * is a "combined" view/controller.
  29.  *
  30.  * @version 1.2 01/30/98
  31.  * @author Georges Saab
  32.  * @author David Karlton
  33.  */
  34.  
  35. public class BasicMenuUtilities {
  36.  
  37.  
  38.  
  39.     /**
  40.      * Returns the subcomponent at the given coordinates in this 
  41.      * menubar's coordinate space.
  42.      */
  43.  
  44.     public static Component getMenuComponentAt(JMenuBar menuBar, int x, int y) {
  45.     // System.out.println("Start Coords: " + x + " " + y);
  46.     int ncomponents = menuBar.getComponentCount();
  47.     Component[] component = menuBar.getComponents();
  48.     for (int i = 0 ; i < ncomponents ; i++) {
  49.         Component comp = component[i];
  50.         Point tp = getTranslatedPoint(menuBar, x, y, menuBar, comp);
  51.         // System.out.println("Item Coords: " + tp.x + " " + tp.y);
  52.         Component ret;
  53.         if (comp instanceof JMenu) {
  54.         ret = getMenuComponentAt((JMenu)comp, tp.x, tp.y);
  55.         } else {
  56.         ret = comp.getComponentAt(tp.x, tp.y);
  57.         }
  58.         // Subcomponent must be visible!
  59.         if ((ret != null) && ret.isShowing()) return ret;
  60.     }
  61.     return null;
  62.     }
  63.  
  64.  
  65.     /*
  66.      * Returns the menu subcomponent at the given coordinates in this 
  67.      * menu's coordinate space.
  68.      * @param x the x coordinate to be tested
  69.      * @param y the y coordinate to be tested
  70.      */
  71.  
  72.     public static Component getMenuComponentAt(JMenu menu, int x, int y) {
  73.     // Coordinates are within the menu's JMenuItem
  74.     if (menu.contains(x,y)) {
  75.         return menu;
  76.     } else {
  77.         // Test the popup menu
  78.         if (menu.isSelected()) {
  79.         Point tp = translateToPopupMenu(menu, x,y);
  80.         return getMenuComponentAt(menu.getPopupMenu(), tp.x, tp.y);
  81.         }
  82.         return null;
  83.     }
  84.     }
  85.  
  86.     /**
  87.      * Returns the menu subcomponent at the given coordinates in this 
  88.      * menu's coordinate space.
  89.      */
  90.  
  91.     public static Component getMenuComponentAt(JPopupMenu popupMenu, int x, int y) {
  92.     int ncomponents = popupMenu.getComponentCount();
  93.     Component[] component = popupMenu.getComponents();
  94.     for (int i = 0 ; i < ncomponents ; i++) {
  95.         Component comp = component[i];
  96.         Point tp = getTranslatedPoint(popupMenu, 
  97.                                x, y, popupMenu, comp);
  98.         Component ret;
  99.         if (comp instanceof JMenu) {
  100.         ret = getMenuComponentAt((JMenu)comp, tp.x, tp.y);
  101.         } else {
  102.         ret = comp.getComponentAt(tp.x, tp.y);
  103.         }
  104.         // Subcomponent must be visible!
  105.         if ((ret != null) && ret.isShowing()) return ret;
  106.     }
  107.     // Not in a child, is it within PopupMenu's insets?
  108.     if (popupMenu.contains(x,y) && popupMenu.isShowing()) {
  109.         // System.out.println("In PopupMenu Insets");
  110.         return popupMenu;
  111.     }
  112.     return null;
  113.     }
  114.  
  115.     /**
  116.      * Returns the given point in the <i>from</i> components coordinate 
  117.      * space translated into the <i>to</i> components coordinate space.  
  118.      * The <i>from</i> component is expected to be a menu-parent of
  119.      * the <i>to</i> component.
  120.      */
  121.  
  122.     public static Point getTranslatedPoint(JMenuBar menuBar, int x, int y, 
  123.                     Component from, Component to) {
  124.     Component c;
  125.     int x1 = x;
  126.     int y1 = y;
  127.     for(c = to; c != null && c != from; c = c.getParent()) {
  128.         Rectangle r = c.getBounds();
  129.         x1 -= r.x;
  130.         y1 -= r.y;
  131.     }
  132.     // Inside Menu
  133.     if (c != null) {
  134.         return new Point(x1, y1);
  135.     }
  136.  
  137.     // Handle submenus
  138.     int ncomponents = menuBar.getComponentCount();
  139.     Component[] component = menuBar.getComponents();
  140.     for (int i = 0 ; i < ncomponents ; i++) {
  141.         Component comp = component[i];
  142.         if (comp instanceof JMenu) {
  143.         Point tp = getTranslatedPoint((JMenu)comp, x, y, comp, to);
  144.         if (tp!= null) return tp;
  145.         }
  146.     }
  147.     return null;
  148.     }
  149.  
  150.     /**
  151.      * Returns the given point in the <i>from</i> components coordinate 
  152.      * space translated into the <i>to</i> components coordinate space.  
  153.      * The <i>from</i> component is expected to parent the <i>to</i> 
  154.      * component.
  155.      */
  156.     public static Point getTranslatedPoint(JPopupMenu popupMenu,
  157.                          int x, int y, 
  158.                          Component from, Component to) {
  159.     Component c;
  160.     int x1 = x;
  161.     int y1 = y;
  162.     for(c = to; c != null && c != from; c = c.getParent()) {
  163.         Rectangle r = c.getBounds();
  164.         x1 -= r.x;
  165.         y1 -= r.y;
  166.     }
  167.     // Inside Menu
  168.     if (c != null) {
  169.         return new Point(x1, y1);
  170.     }
  171.  
  172.     // Handle submenus
  173.     int ncomponents = popupMenu.getComponentCount();
  174.     Component[] component = popupMenu.getComponents();
  175.     for (int i = 0 ; i < ncomponents ; i++) {
  176.         Component comp = component[i];
  177.         if (comp instanceof JMenu) {
  178.         Point tp = getTranslatedPoint((JMenu)comp, x, y, comp, to);
  179.         if (tp!= null) return tp;
  180.         }
  181.     }
  182.     return null;
  183.     }
  184.  
  185.     /**
  186.      * Returns the given point in the <i>from</i> components coordinate 
  187.      * space translated into the <i>to</i> components coordinate space.  
  188.      * The <i>from</i> component is expected to be a menu-parent of
  189.      * the <i>to</i> component.
  190.      */
  191.  
  192.     public static Point getTranslatedPoint(JMenu menu, 
  193.                            int x, int y, 
  194.                            Component from, Component to) {
  195.     // System.out.println("In coords: " + x + " " + y);
  196.     if (to == menu) {
  197.         return new Point (x,y);
  198.     } else {
  199.         if (menu.isSelected()) {
  200.         Point tp = translateToPopupMenu(menu, x,y);
  201.         // System.out.println("PopupMenu coords: " + tp.x + " " + tp.y);
  202.         int ncomponents = menu.getMenuComponentCount();
  203.         Component[] component = menu.getMenuComponents();
  204.         for (int i = 0 ; i < ncomponents ; i++) {
  205.             Component comp = component[i];
  206.             JPopupMenu popupMenu = menu.getPopupMenu();
  207.             Point tp2 = getTranslatedPoint(popupMenu, tp.x, tp.y,
  208.                              popupMenu, comp);
  209.             // System.out.println("Item Coords: " + tp2.x + " " + tp2.y);
  210.             if (comp == to) 
  211.             return tp2;
  212.             if (comp instanceof JMenu) {
  213.             Point tp3 = getTranslatedPoint((JMenu)comp,
  214.                                tp2.x, tp2.y, comp, to);
  215.             if (tp3!= null) return tp3;
  216.             }
  217.         }
  218.         }
  219.         return null;
  220.     }
  221.     }
  222.  
  223.     /**
  224.      * Returns a point in the coordinate space of this menu's popupmenu
  225.      * which corresponds to the point p in the menu's coordinate space.
  226.      * @param p the point to be translated
  227.      */
  228.     private static Point translateToPopupMenu(JMenu menu, Point p) {
  229.     return translateToPopupMenu(menu, p.x, p.y);
  230.     }
  231.  
  232.     /**
  233.      * Returns a point in the coordinate space of this menu's popupmenu
  234.      * which corresponds to the point (x,y) in the menu's coordinate space.
  235.      * @param x the x coordinate of the point to be translated
  236.      * @param y the y coordinate of the point to be translated
  237.      */
  238.     private static Point translateToPopupMenu(JMenu menu, int x, int y) {
  239.         int newX;
  240.         int newY;
  241.  
  242.         if (menu.getParent() instanceof JPopupMenu) {
  243.         newX = x - menu.getSize().width;
  244.         newY = y;
  245.         } else {
  246.         newX = x;
  247.         newY = y - menu.getSize().height;
  248.         }
  249.  
  250.         return new Point(newX, newY);
  251.     }
  252.  
  253. }
  254.